package cn.sjx.org.controller;

import cn.sjx.auth.annotation.Permission;
import cn.sjx.basic.utils.AjaxResult;
import cn.sjx.basic.utils.PageList;
import cn.sjx.org.domain.Department;
import cn.sjx.org.domain.Employee;
import cn.sjx.org.domain.EmployeeRoleDTO;
import cn.sjx.org.query.DepartmentQuery;
import cn.sjx.org.query.EmployeeQuery;
import cn.sjx.org.service.EmployeeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@Permission(name = "员工管理" ,descs = "员工管理权限")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;


    @Permission(name = "通过ID查询" ,descs = "通过ID查询员工")
    //查询单个
    @ApiOperation(value = "通过ID查询" )
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable Serializable id) {
        try {
            Employee employee = employeeService.getById(id);
            return AjaxResult.me().setData(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败");
        }
    }


    @Permission(name = "新增/修改" ,descs = "新增/修改员工")
    @PutMapping
    @ApiOperation(value = "新增或更新" )
    public AjaxResult insertOrupdate(@RequestBody Employee employee) {
        //通过Objects.isNull来判断
        try {
            if (Objects.isNull(employee.getId())) {
                employeeService.insert(employee);
                return AjaxResult.me();
            } else {
                employeeService.update(employee);
                return AjaxResult.me();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败");
        }
    }


    @Permission(name = "删除" ,descs = "删除员工")
    //按ID删除
    @ApiOperation(value = "通过ID删除" )
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable Serializable id){
        try {
            employeeService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败");
        }
    }


    @Permission(name = "分页查询" ,descs = "分页查询员工")
    @PostMapping
    public AjaxResult pageList(@RequestBody EmployeeQuery employeeQuery){
        PageList<Employee> pageList = employeeService.pageList(employeeQuery);
        return AjaxResult.me().setData(pageList);

    }

    @Permission(name = "分配角色" )
    @PostMapping("/setRole")
    public AjaxResult setRole(@RequestBody EmployeeRoleDTO employeeRoleDTO){
         employeeService.setRole(employeeRoleDTO);
        return AjaxResult.me();

    }
    @Permission(name = "批量删除" ,descs = "批量删除员工")
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }


    @GetMapping
    public AjaxResult getAll() {
        try {
            List<Employee> list = employeeService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            return AjaxResult.me().setSuccess(false).setMessage("失败");
        }

    }
}
